{extend name="public:layout" /}
{block name="title"}添加优惠套装{/block}
{block name="head"}{/block}
{block name="body"}
<div class="wrapper wrapper-content">
    <div class="ibox">
        <div class="ibox-title">
            <ol class="breadcrumb">
                <li><i class="fa fa-hand-o-right"></i> <a href="{:url('index')}"> 套装列表</a></li>
                <li class="active">添加优惠套装</li>
            </ol>
        </div>
        <div class="ibox-content">
            <form method="post" class="form-horizontal" id="form">
                <div class="form-group">
                    <label class="col-sm-2 control-label Validform_label">套装名称</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="请输入套装名称" name="bl_name" value="" datatype="*" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label Validform_label">优惠套装价格</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" readonly id="bl_discount_price" name="bl_discount_price" value="0" datatype="*" />
                        <input type="hidden" name="total_simple_price" value="" />
                        <p class="help-block">原价<span id="total_simple_price">0</span>元 (已添加搭配商品的默认价格总计)</p>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label Validform_label">套装商品</label>
                    <div class="col-sm-10">
                        <a class="btn btn-info" onclick="showMoreGoods(this,setMoreGoods);"
                           data-field="sku_ids"
                           data-siteid="{$site_id}"
                           data-url="{:url('plus/Goods/selectmore')}"
                        >选择商品</a>
                        <div id="goodsList"></div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label Validform_label">运费承担</label>
                    <div class="col-sm-4">
                        <label class="radio-inline i-checks">
                            <input type="radio" name="bl_freight_choose" value="0" checked /> 卖家承担
                        </label>
                        <label class="radio-inline i-checks">
                            <input type="radio" name="bl_freight_choose" value="1" /> 买家承担
                        </label>
                    </div>
                </div>
                <div class="form-group" id="freight" style="display:none;">
                    <label class="col-sm-2 control-label Validform_label">运费</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="请输入活动名称" name="bl_freight" value="0" datatype="*" />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <button class="btn btn-primary" type="button" id="btnSub">保存内容</button>
                        <a href="{:url('index')}" class="btn btn-white">取消</a>
                        <span id="validError"></span>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script id="set-goods-tpl" type="text/html">
    <table class="table table-bordered">
        <thead>
        <tr>
            <th width="50">#</th>
            <th width="300">商品名称</th>
            <th width="100">原价</th>
            <th width="100">优惠价格</th>
            <th width="40">操作</th>
        </tr>
        </thead>
        <tbody>
        {{ each sku_list }}
        <tr>
            <td>
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][site_id]" value="{{ $value.site_id }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][site_name]" value="{{ $value.site_name }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][goods_id]" value="{{ $value.goods_id }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][sku_id]" value="{{ $value.sku_id }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][goods_name]" value="{{ $value.goods_name }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][sku_name]" value="{{ $value.sku_name }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][goods_thumb]" value="{{ $value.goods_thumb }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][simple_price]" value="{{ $value.simple_price }}" />
                {{ $value.sku_id }}
            </td>
            <td>
                <img src="{{ $value.goods_thumb }}" class="pull-left" style="width:32px;height:32px;margin:4px;" />
                {{ $value.goods_name }}
            </td>
            <td>{{ $value.simple_price }}</td>
            <td><input type="text" class="form-control bl_goods_price" onblur="calcPrice()" name="sku_list[{{ $value.sku_id }}][bl_goods_price]" value="{{ $value.simple_price }}" /></td>
            <td><a href="javascript:;" onclick="delGoods(this)">移除</a></td>
        </tr>
        {{ /each }}
        </tbody>
    </table>
</script>
{/block}
{block name="script"}
<script type="text/javascript">
    $(function () {
        iCheck();
        $("input[name='bl_freight_choose']").on('ifChecked', function (event) {
            if($(this).val()==1){
                $('#freight').show();
            }else{
                $('#freight').hide();
            }
        });
        In('selectgoods');
        ajaxSubForm('#btnSub');
    })
    //选取商品回调
    function setMoreGoods(field,sku) {
        In('layer','templete',function () {
            var html = template('set-goods-tpl', sku);
            $('#goodsList').html(html);
            calcPrice();
        })
    }
    //计算价格
    function calcPrice(){
        var bl_discount_price=0;
        var total_simple_price=0;
        $("#goodsList tbody").find("tr").each(function(){
            var tdArr = $(this).children();
            var simple_price = tdArr.eq(2).text(); //原价
            var bl_goods_price = tdArr.eq(3).find('input').val(); //优惠价
            bl_discount_price += parseFloat(bl_goods_price);
            total_simple_price += parseFloat(simple_price);
        });
        $('#total_simple_price').text(total_simple_price);
        $("input[name='total_simple_price']").val(total_simple_price);
        $('#bl_discount_price').val(bl_discount_price);
    }
    //删除商品
    function delGoods(obj) {
        $(obj).closest('tr').remove();
        calcPrice();
    }
</script>
{/block}